//
//  SetVC_setUser.m
//  Login
//
//  Created by ZF on 17/2/27.
//  Copyright © 2017年 ZF. All rights reserved.
//

#import "SetVC_setUser.h"
#import "NetworkLink.h"
#import "LoginVC.h"
#import "SetIDViewController.h"
#import "SetPWViewController.h"
#import "SetDepartmentViewController.h"

@interface SetVC_setUser () <UITableViewDataSource,UITableViewDelegate>
{
    AppDelegate *delegate;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation SetVC_setUser
- (void)viewDidLoad {
    [super viewDidLoad];
    delegate = [Helps app];
    self.tableView.scrollEnabled = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onFresh) name:@"fresh" object:nil];
   
}

- (void)onFresh
{
    [self.tableView reloadData];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark － 段落行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 1)
    {
        return 2;
    }
    else if(section == 2)
    {
        return 1;
    }
    return 3;
}

#pragma mark - Cell内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = COLOR(215, 215, 215, 1);
    view.frame = CGRectMake(0, 43, tableView.frame.size.width, 1);
    if(indexPath.section == 0)
    {
        if(indexPath.row == 0)
        {
            cell.textLabel.text = @"ID";
            cell.detailTextLabel.text = delegate.userID;
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
            cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        else if(indexPath.row == 1)
        {
            cell.textLabel.text = @"昵称";
            cell.detailTextLabel.text = delegate.userName;
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
            cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        else if(indexPath.row == 2)
        {
            cell.textLabel.text = @"部门";
            cell.detailTextLabel.text = delegate.department;
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
            cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    
    if(indexPath.section == 1)
    {
        if(indexPath.row == 0)
        {
            cell.textLabel.text  = @"修改密码";
            cell.textLabel.font = [UIFont systemFontOfSize:15];
        }
        else if(indexPath.row == 1)
        {
            cell.textLabel.text = @"换个用户";
            cell.textLabel.font = [UIFont systemFontOfSize:15];
        }
    }
    if(indexPath.section == 2)
    {
        view.backgroundColor = [UIColor whiteColor];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"注销" forState:UIControlStateNormal];
        [btn setTintColor:[UIColor whiteColor]];
        [btn setBackgroundColor:[UIColor redColor]];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        btn.layer.cornerRadius = 5.0;
        btn.frame = CGRectMake(8, 3, ScreenWidth - 16, cell.frame.size.height - 6);
        [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn addTarget:self action:@selector(onTouchDown:) forControlEvents:UIControlEventTouchDown];
        cell.selected = NO;
        [cell addSubview:btn];
    }
    [cell addSubview:view];
    return cell;
}

- (void)onClick:(UIButton *)sender
{
    sender.backgroundColor = [UIColor redColor];
    [NetworkLink logOffUserComplete:^(BOOL ok) {
        if(ok == YES)
        {
            [delegate.userdefault setBool:NO forKey:@"IfLogin"];
            [delegate.userdefault setObject:@"" forKey:@"UserName"];
            [delegate.userdefault setObject:@"" forKey:@"Password"];
            [delegate.userdefault synchronize];

            LoginVC *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"login"];
            vc.navigationItem.hidesBackButton = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }];
}

- (void)onTouchDown:(UIButton *)sender
{
    sender.backgroundColor = COLOR(255, 98, 109, 1);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
    view.backgroundColor = COLOR(230, 230, 230, 1);
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 1 || section == 2)
    {
        return 30;
    }
    return 0;
}

#pragma mark － 选中Cell操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        if(indexPath.row == 1)
        {
            SetIDViewController *con = [self.storyboard instantiateViewControllerWithIdentifier:@"setID"];
            [self.navigationController pushViewController:con animated:YES];
        }
        if(indexPath.row == 2)
        {
            [NetworkLink userDepartmentListComplete:^(DepartmentList *d) {
                
            }];
        }
    }else if(indexPath.section == 1)
    {
        if(indexPath.row == 0)
        {
            SetPWViewController *con = [self.storyboard instantiateViewControllerWithIdentifier:@"setPW"];
            [self.navigationController pushViewController:con animated:YES];
        }
        if(indexPath.row == 1)
        {
            LoginVC *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"login"];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
